//
//  SeemeDataVC.m
//  SelfDriving
//
//  Created by apple on 16/1/20.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "SeemeDataVC.h"
#import "MemberRoamingCell.h"
#import "BusinessService+WD.h"
@interface SeemeDataVC ()<BaseCollectionDelegate>
@property (weak, nonatomic) IBOutlet BaseCollection *collection;

@end

@implementation SeemeDataVC
{
    NSMutableArray *data;
    UILabel *notlabel;//无数据
    UIView *notview;
    NSString *_total;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    _collection.bdelegate = self;
    data = [[NSMutableArray alloc]init];
    
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(scollTop:)
     name:@"scollTop"
     object:nil];
}
-(void)scollTop:(NSNotification *)not{
    if (data.count!=0) {
        [_collection scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]
                            atScrollPosition:UICollectionViewScrollPositionTop
                                    animated:YES];
    }
}
-(void)initData:(int)index{
        
        NSMutableDictionary *dic = [UserDefaults readUserDefaultObjectValueForKey:@"userLocation"];
        [dic setValue:@"0" forKey:@"type"];
        [dic setObject:[NSNumber numberWithInt:index] forKey:@"page"];
        [dic setObject:[AppDelegate shareDelegate].user.uid forKey:@"fid"];
    
        WeakSelf
        [[BusinessService shareInstance]getdynamic_guest:dic success:^(id result) {
            if ([[AppDelegate shareDelegate].user.memberlevel isEqualToString:@"0"]) {
                
//                weakSelf.title = [NSString stringWithFormat:@"动态(%@)",result[@"total"]];
                if (!([(NSMutableArray *)result count] == 0)) {
                    _total = result[@"total"];
                }else{
                    _total = 0;
                }
                
                UIButton *btn =  [weakSelf.switchView.topScrollView viewWithTag:100];
                NSString *title = _total == 0 ? @"资料" : [NSString stringWithFormat:@"资料(%@)",_total];
                [btn setTitle:title forState:UIControlStateNormal];
                
                [self notdongtai];
            }else{
                data = [_collection result:result[@"list"] olddata:data index:index];
                [_collection reloadData];
            }
            
        } failure:^(id result) {
            [SVProgressHUD showErrorWithStatus:result];
        }];
        
    
}
#pragma  mark 无动态数据时
-(void)notdongtai{
    _collection.hidden = YES;
    notview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCRE_WIDTH, _collection.frame.size.height)];
    [self.view addSubview:notview];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(SCRE_WIDTH/2-40, 44, 80, 80)];
    img.image = [UIImage imageNamed:@"skhead"];
    [notview addSubview:img];
    
    
    UILabel *hi = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCRE_WIDTH, 30)];
    hi.textAlignment = NSTextAlignmentCenter;
//    hi.text = @"hi,嘿";
    hi.text = [NSString stringWithFormat:@"Hi,%@",[AppDelegate shareDelegate].user.nickname];
    [notview addSubview:hi];
    
    UILabel *fj = [[UILabel alloc]initWithFrame:CGRectMake(0, 160, SCRE_WIDTH, 30)];
    fj.textAlignment = NSTextAlignmentCenter;
    
    fj.text = [_total intValue] == 0 ? @"没有人看过你" : [NSString stringWithFormat:@"有%@人来看过你",_total];
    
    [notview addSubview:fj];
    
    UILabel *kq = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, SCRE_WIDTH, 30)];
    kq.textAlignment = NSTextAlignmentCenter;
    kq.text = @"快去附近主动结识朋友";
    [notview addSubview:kq];
    
    UILabel *sk = [[UILabel alloc]initWithFrame:CGRectMake(0, 240, SCRE_WIDTH, 100)];
    sk.textAlignment = NSTextAlignmentCenter;
    sk.text = @"谁看过我只记录会员期间的用户超过3天未登录，将不会出现在附近更新资料，加入群组，发布动态将让你更受欢迎";
    sk.font = [UIFont systemFontOfSize:14];
    sk.textColor = [UIColor grayColor];
    sk.numberOfLines = 0;
    [notview addSubview:sk];
    
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(0, _collection.frame.size.height-40, SCRE_WIDTH, 40)];
    [btn setTitle:@"成为会员，立即开启访客信息" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor blackColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(tovip:) forControlEvents:UIControlEventTouchUpInside];
    [notview addSubview:btn];
    
}
-(void)tovip:(UIButton *)btn{

    [[NSNotificationCenter defaultCenter ]postNotificationName:@"toVipAction" object:nil];

    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)sectio{
    return data.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MemberRoamingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MemberRoamingCell" forIndexPath:indexPath];
    LoginResponseModel *user = [data objectAtIndex:indexPath.row];
    cell.name_label.text = user.nickname;
    cell.name_label.textColor = [UIColor whiteColor];
    cell.vip_img.hidden = YES;
    [ValidateHelper setImage:user.headsmall img:cell.head_img istoux:NO];
    NSString *age = [ValidateHelper timeToAge:user.age];
    NSString *dis = [ValidateHelper DistanceFormat:user.distance];
    NSString *time = [ValidateHelper timeStringWith:[user.viewtime doubleValue] multiLine:NO];
    NSString *sign = [NSString stringWithFormat:@"%@岁 %@ %@",age,dis,time];
    cell.sign_lable.text = sign;
    cell.sign_lable.font = [UIFont systemFontOfSize:10];
//    cell.sign_lable.hidden = YES;
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake((SCRE_WIDTH-30)/2, (SCRE_WIDTH-30)/2);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LoginResponseModel *model = [data objectAtIndex:indexPath.row];
    
    DTModel *mode = [[DTModel alloc]init];
    mode.uid = model.uid;
    [[NSNotificationCenter defaultCenter ]postNotificationName:@"togerenAction" object:mode];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
